// 获取当前页面的 URL
function getCurrentUrl() {
  console.log("获取页面url");

  return window.location.href;
}
let isBig = false;
// 打印 URL
function logUrl(url) {
  console.log("Current URL:", url);
}

// 创建 iframe 并设置 src
function createIframe(url) {
  const obj = document.getElementById("video");
  if (obj == null) {
    console.log("meiyou1");
    return;
  }
  if (obj) {
    // 使用 while 循环删除所有子元素
    while (obj.firstChild) {
      obj.removeChild(obj.firstChild);
    }
  }
  const iframe = document.createElement("iframe");
  iframe.src = "https://jx.xmflv.com/?url=" + url;
  iframe.style.width = "100%";
  iframe.style.height = "100%";
  iframe.style.position = "relative";
  obj.appendChild(iframe);
  const div = document.createElement("div");
  div.id = "my_quan_p";
  //   div.style.backgroundColor = "red";
  div.style.width = "50px";
  div.style.height = "50px";
  div.style.position = "absolute"; // 使用驼峰命名法
  div.style.zIndex = "1000";
  div.style.right = "0"; // 使用驼峰命名法
  div.style.bottom = "0"; // 使用驼峰命名法
  obj.appendChild(div);
  div.addEventListener("click", function () {
    isBig = !isBig;
    if (isBig) {
      let ob = document.getElementsByClassName("NavHeader_navheader__4yBbz");
      if (ob && ob[0]) {
        ob[0].style.display = "none";
      }
      document.getElementById("tvg").style.display = "none";
    } else {
      let ob = document.getElementsByClassName("NavHeader_navheader__4yBbz");
      if (ob && ob[0]) {
        ob[0].style.display = "flex";
      }
      document.getElementById("tvg").style.display = "block";
    }
  });
}

// 初始化时执行一次
logUrl(getCurrentUrl());

// 监听 URL 变化
let oldHref = document.location.href;
window.onload = function () {
  const bodyList = document.querySelector("body");
  const observer = new MutationObserver(function (mutations) {
    mutations.forEach(function (mutation) {
      if (oldHref !== document.location.href) {
        oldHref = document.location.href;
        logUrl(oldHref);
        createIframe(oldHref);
      }
    });
  });
  const config = {
    childList: true,
    subtree: true,
  };
  observer.observe(bodyList, config);
};
const video = document.querySelector(".iqp-player-videolayer-inner video");
video?.addEventListener("play", function () {
  console.log("video zt");
  this.pause();
  createIframe(getCurrentUrl());
});
